package com.miracle.common.state.user;

import android.content.Context;

import com.miracle.common.bean.User.User;
import com.miracle.common.util.PreferencesUtils;
import com.miracle.common.util.Utils;

import static com.miracle.common.util.Utils.getSafetyString;

/**
 * com.miracle.common.state.user
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/6/30 9:42
 * @see com.miracle.common.state.user
 */
public class LoginState implements UserState {

    @Override
    public void setUser(Context context, User user) {
        PreferencesUtils.setPreferences(context, PreferencesUtils.State.USER_NAME, user
                .getUserName());
        PreferencesUtils.setPreferences(context, PreferencesUtils.State.USER_ROLE, user
                .getRole());
        PreferencesUtils.setPreferences(context, PreferencesUtils.State.USER_SESSIONID, user
                .getJSESSIONID());
        PreferencesUtils.setPreferences(context, PreferencesUtils.State.USER_TEL, getSafetyString
                (user
                        .getPhone()));
        PreferencesUtils.setPreferences(context, PreferencesUtils.State.USER_MAIL,
                getSafetyString(user
                        .getEmail()));
        PreferencesUtils.setPreferences(context, PreferencesUtils.State.USER_SEX, getSafetyString
                (user
                        .getSex()));
        PreferencesUtils.setPreferences(context, PreferencesUtils.State.USER_PATH,
                getSafetyString(user
                        .getPath()));
    }

    @Override
    public User getUser(Context context) {
        User user = new User();
        String name = PreferencesUtils.getPreferences(context, PreferencesUtils.State.USER_NAME);
        String role = PreferencesUtils.getPreferences(context, PreferencesUtils.State.USER_ROLE);
        String sessionId = PreferencesUtils.getPreferences(context, PreferencesUtils.State
                .USER_SESSIONID);
        String tel = PreferencesUtils.getPreferences(context, PreferencesUtils.State.USER_TEL);
        String mail = PreferencesUtils.getPreferences(context, PreferencesUtils.State.USER_MAIL);
        String sex = PreferencesUtils.getPreferences(context, PreferencesUtils.State.USER_SEX);
        String path = PreferencesUtils.getPreferences(context, PreferencesUtils.State.USER_PATH);

        user.setUserName(name);
        user.setRole(role);
        user.setJSESSIONID(sessionId);
        user.setPhone(tel);
        user.setEmail(mail);
        user.setSex(sex);
        user.setPath(path);
        return user;
    }
}
